//
//  CXCollectionViewTestVC.swift
//  CXSwiftDemo
//
//  Created by shenjie on 2023/12/9.
//
//控制器 
import UIKit

class CXCollectionViewTestVC: UIViewController {

    fileprivate var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "SupplementaryView"
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .vertical//滚动方向
        //属性设置为true，则分组头有悬停效果
        flowLayout.sectionHeadersPinToVisibleBounds = true
        //属性设置为true，则分组脚有悬停效果
        flowLayout.sectionFootersPinToVisibleBounds = true
        
        //初始化与坐标
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), collectionViewLayout: flowLayout)
        //是否显示水平滚动条
        collectionView.showsHorizontalScrollIndicator = false
        //是否开启分页
        collectionView.isPagingEnabled = false
        //是否允许回弹
        collectionView.bounces = true
        // 注册 Cell
        collectionView.register(CXTestCollectionViewCell.self, forCellWithReuseIdentifier: "CellID")
        // 注册头部视图
        collectionView.register(CXCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView")
        // 注册尾部视图
        collectionView.register(CXCollectionFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footerView")
        
        collectionView.delegate = self
        collectionView.dataSource = self
        self.view.addSubview(collectionView)
    }
}

//代理
extension CXCollectionViewTestVC: UICollectionViewDelegateFlowLayout {

    //行最小间距（同一行 item 之间的水平间距）
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 50
    }

    //列最小间距（相邻两行同一列 item 之间的垂直间距）
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 30
    }
    
    //设置整个collectionView的内边距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 20, left: 15, bottom: 20, right: 15)
    }
    
    //返回cell尺寸
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //15是左右边距，50是cell之间的距离
        let cellWidth = (UIScreen.main.bounds.size.width - 15 - 15 - 50 * 2 ) / 3
        // 返回 cell 尺寸
        return CGSize(width: cellWidth, height: 120)
    }
    
}

//代理
extension CXCollectionViewTestVC: UICollectionViewDelegate {
    //cell点击事件
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("点击了cell")
    }
}

//代理
extension CXCollectionViewTestVC: UICollectionViewDataSource {
    //返回多少组
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 4
    }
    //每组多少个item
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    
    //设置cell上的UI
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellID", for: indexPath) as! CXTestCollectionViewCell
//        cell.backgroundColor = UIColor(red:  CGFloat(arc4random()%256)/256.0, green:  CGFloat(arc4random()%256)/256.0, blue:  CGFloat(arc4random()%256)/256.0, alpha: 1)
        return cell
    }
    
    //设置组头组脚，供 UICollectionView 加载。注意：Header 和 Footer 出现一定是配对的
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            //组头
            let headerView: CXCollectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView", for: indexPath) as! CXCollectionHeaderView
            return headerView
        } else if kind == UICollectionView.elementKindSectionFooter {
            //组脚
            let footerView: CXCollectionFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footerView", for: indexPath) as! CXCollectionFooterView
            return footerView
        }

        return UICollectionReusableView()
    }
    
    //设置组header size
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.size.width, height: 50)
    }
    //设置组Footer size
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.size.width, height: 50)
    }

}

